import http from './http';

// 获取所有书籍
export const getAllBooks = async (page = 0, size = 10, name = '') => {
  try {
    const response = await http.get(`/books?page=${page}&size=${size}${name ? `&name=${name}` : ''}`);
    return response;
  } catch (error) {
    throw error;
  }
};

// 搜索书籍
export const searchBooks = async (keyword, page = 0, size = 10) => {
  try {
    const response = await http.get(`/books?page=${page}&size=${size}&name=${encodeURIComponent(keyword)}`);
    return response;
  } catch (error) {
    throw error;
  }
};

// 获取书籍详情
export const getBookById = async (id) => {
  try {
    const response = await http.get(`/books/${id}`);
    return response;
  } catch (error) {
    throw error;
  }
};

// 创建书籍（管理员功能）
export const createBook = async (bookData) => {
  try {
    const response = await http.post('/books', bookData);
    return response;
  } catch (error) {
    throw error;
  }
};

// 更新书籍（管理员功能）
export const updateBook = async (id, bookData) => {
  try {
    const response = await http.put(`/books/${id}`, bookData);
    return response;
  } catch (error) {
    throw error;
  }
};

// 删除书籍（管理员功能）
export const deleteBook = async (id) => {
  try {
    const response = await http.delete(`/books/${id}`);
    return response;
  } catch (error) {
    throw error;
  }
}; 